﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Booking
{
    /// <summary>
    /// 车辆信息列表
    /// </summary>
    public partial class SelectAutomobileView : UserControl, IView
    {
        #region Property
        //客户
        WebRef.VCustomer model = null;
        Promise _promise = new Promise();
        #endregion

        #region Method

        public SelectAutomobileView()
        {
            InitializeComponent();
        }

        public SelectAutomobileView(WebRef.VCustomer model)
        {
            InitializeComponent();
            this.model = model;
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void RefreshData()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                ListResult<WebRef.VAutomobile> result = new Service.QueryService().QueryAutomobileList(model.Id);
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        ctlGrid.DataSource = result.Data.Items;
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        #endregion

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("back", Resources.isBack, "返回", ctlBtnCancel_Click))
                 .AddAction(new ActionNode("create", Resources.isCreateCar, "新增", ctlBtnCreate_Click))
                 .AddAction(new ActionNode("create", Resources.isEdit, "修改", ctlBtnEdit_Click))
                 .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_Click))
                 .AddAction(new ActionNode("next", Resources.isNextStep, "下一步", ctlBtnNext_Click));
        }

        public void Active(ViewActiveArgs args)
        {
            _promise.Then(RefreshData)
                .Run(null);
        }

        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                if (args.ClosedArgs.View is Create.CreateBookingBillView)
                {
                    UIContext.Current.ViewManager.CloseView(true, null);
                }
                else
                {
                    RefreshData();
                }
            }
        }

        public ViewHeader GetHeader()
        {
            return new ViewHeader("车辆信息列表");
        }
        #endregion

        #region Action
        /// <summary>
        /// 新增车辆信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnCreate_Click(object sender, object e)
        {
            if (model != null)
            {
                UIContext.Current.ViewManager.PopupView(new Create.CreateAutomobileView(model), new PopupViewArgs("新增车辆资料"));
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        private void ctlBtnEdit_Click(object sender, object e)
        {
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VAutomobile item = this.GVData.GetFocusedRow() as WebRef.VAutomobile;
                if (item != null)
                {
                    UIContext.Current.ViewManager.PopupView(new Edit.UpdateBookingAutoMobileView(model, item), new PopupViewArgs("修改车辆信息"));
                }
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnCancel_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, object e)
        {
            RefreshData();
        }

        /// <summary>
        /// 下一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnNext_Click(object sender, object e)
        {
            WebRef.VAutomobile entity = GVData.GetRow(GVData.FocusedRowHandle) as WebRef.VAutomobile;
            if (entity == null)
            {
                return;
            }
            UIContext.Current.ViewManager.SwitchView(new Create.CreateBookingBillView(model, entity));
        }
        #endregion
    }
}
